package com.soft.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Dept;
import com.soft.entity.Emp;
import com.soft.mapper.DeptMapper;
import com.soft.mapper.EmpMapper;
@Service
public class EmpService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    public List<Emp> queryByDeptName(String deptName)
    {
    	QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
    	deptWrapper.eq("dept_name",deptName);
    	Dept dept = deptMapper.selectOne(deptWrapper);
    	QueryWrapper<Emp> empWrapper = new QueryWrapper<>();
    	empWrapper.eq("deptno", dept.getDeptno());
    	List<Emp> empList = empMapper.selectList(empWrapper);
    	return empList;
    }
    public int insertEmp(String empId,String empname,String deptName,String position,String hiredate)
    {
    	QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
    	deptWrapper.eq("dept_name",deptName);
    	Dept dept = deptMapper.selectOne(deptWrapper);
    	Emp emp = new Emp();
    	emp.setEmpno(empId);
    	emp.setEmp_name(empname);
    	emp.setDeptno(dept.getDeptno());
    	emp.setPosition(position);
    	emp.setHire_date(hiredate);
    	int num = empMapper.insert(emp);
    	return num;
    }
    public int updateEmpTest(String empname,String position,String deptName)
    {
        QueryWrapper<Dept> deptWrapper = new QueryWrapper<>();
        deptWrapper.eq("dept_name", deptName);
        Dept dept = deptMapper.selectOne(deptWrapper);
        

        UpdateWrapper<Emp> empUpdateWrapper = new UpdateWrapper<>();
        empUpdateWrapper.eq("emp_name", empname)
        				.eq("deptno",dept.getDeptno())
                        .set("position", position);  
        
        int num = empMapper.update(empUpdateWrapper);
        return num;
    	
    }
}